// pages/cart/cart.js
//  把api导进来
const API = require('../../http/api')
Page({
  // 跳转到首页
  goindex() {
    wx.switchTab({
      url: '/pages/index/index',
    })
  },
  // 渲染数据
  async getlist() {
    const token = wx.getStorageSync('token')
    const res = await API.getgoodslists({
      token
    })
    console.log('购物车列表', res);
    if (res.code == 0) {
      this.setData({
        cartlist: res.data.items,
        priceArr: res.data.price * 100
      })
    }
  },
  // 购物车加
  async plus(e) {
    console.log(e.detail);
    // console.log('购物车加', e.currentTarget.dataset.index, e);
    // this.data.number = this.data.cartlist[e.currentTarget.dataset.index].number
    const key = this.data.cartlist[e.currentTarget.dataset.index].key
    let obj = {
      token: wx.getStorageSync('token'),
      number: e.detail,
      key
    }
    const res = await API.getgoodslistjs(obj)
    console.log('数量加加', res, this.data.cartlist[e.detail].number);
  },
  // 选中状态
  async onChange(e) {
    console.log('商品的选择状态', e.detail);
    const key = this.data.cartlist[e.currentTarget.dataset.index].key
    let obj = {
      token: wx.getStorageSync('token'),
      selected: e.detail,
      key
    }
    const res = await API.getgoodslistxz(obj)
    console.log('购物车选择状态', res);
    this.getlist()
  },
  // 删除单个商品
  async del(e) {
    console.log('dele', e.currentTarget.dataset.index);
    const key = this.data.cartlist[e.currentTarget.dataset.index].key
    let obj = {
      token: wx.getStorageSync('token'),
      key
    }
    const res = await API.getgoodslistdg(obj)
    this.getlist()
  },
  /**
   * 页面的初始数据
   */
  data: {
    cartlist: [],
    number: 1,
    priceArr: 0
  },

  onShow() {
    this.getlist()
  },

})